public class Sort {

    //插入排序
    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int ret = arr[i];
            int j = i - 1;
            for (; j >= 0 ; j--) {
                if(arr[j] > ret) {
                    arr[j+1] = arr[j];
                }else {
                    //arr[j+1] = ret;
                    break;
                }
            }
            arr[j+1] = ret;
        }
    }

    //希尔排序
    public static void shellSort(int[] arr) {
        int gap = arr.length / 2;
        while(gap > 0) {
            shell(arr,gap);
            gap /= 2;
        }
    }

    public static void shell(int[] arr,int gap) {
        for (int i = gap; i < arr.length; i++) {
            int ret = arr[i];
            int j = i - gap;
            for (; j >= 0 ; j -= gap) {
                if(arr[j] > ret) {
                    arr[j+gap] = arr[j];
                }else {
                    //arr[j+1] = ret;
                    break;
                }
            }
            arr[j+gap] = ret;
        }
    }

    //选择排序
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int mindex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[mindex] > arr[j]) {
                    mindex = j;
                }
            }
            //交换
            swap(arr,mindex,i);
        }
    }

    //选择排序2
    public static void selectSort2(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while(left < right) {
            int mindex = left;
            int maxdex = left;
            for (int i = left + 1; i <= right ; i++) {
                if(arr[mindex] > arr[i]) {
                    mindex = i;
                }
                if(arr[maxdex] < arr[i]) {
                    maxdex = i;
                }
            }
            swap(arr,mindex,left);
            ////假设第一个值 是最大值
            if(maxdex == left) {
                maxdex = mindex;
            }
            swap(arr,maxdex,right);
            left++;
            right--;
        }
    }

    //堆排序
    public static void heapSort(int[] arr) {
        createHeap(arr);
        int end = arr.length - 1;
        while(end > 0) {
            swap(arr,0,end);
            siftDown(arr,0,end);
            end--;
        }
    }

    //建立大根堆
    public static void createHeap(int[] arr) {
        for (int i = (arr.length-1-1) / 2; i >= 0 ; i--) {
            siftDown(arr,i, arr.length);
        }
    }

    //向下调整
    private static void siftDown(int[] arr,int parent, int k) {
        int child = 2 * parent + 1;
        while(child < k) {
            if(child + 1 < k && arr[child + 1] > arr[child]) {
                child++;
            }
            if(arr[child] > arr[parent]) {
                //交换
                swap(arr,parent,child);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    //冒泡排序
    public static void bubbleSort(int[] arr) {
        //趟数
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flu = false;
            //每一趟的比较的次数
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j+1]) {
                    swap(arr,j,j+1);
                    flu = true;
                }
            }
            if( !flu) {
                break;
            }
        }
    }

    private static void swap(int[] arr,int i,int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    
}
